# coding=utf-8
"""
测试记录控制器
"""
from flask import Blueprint

from rpialert.base.beanret import BeanRet
from rpialert.base.cachedata import CacheData
from rpialert.base.tools.cachedataclient import CacheDataClient
from rpialert.service.testrecordsv import TestRecordSV

alert = Blueprint('screen', __name__)


@alert.route('/screen/<code>')
def detectEnterForScreenTest(code):
    '''
    PUT 直接调用的监测键盘值接口，同时将检测值发送给前端显示控制
    1.判断键盘值是否在需要的范围内
    2.记录测试通过信息
    :param code: 键盘值 一般是数字化的
    :return:
    '''

    # 1.判断键盘值是否在需要的范围内
    cacheData = CacheData().toObj(CacheDataClient().read())
    jobnumber = cacheData.getJobnumber
    testOptionCode = 'screen'
    if int(code) == cacheData.getDetectEnter:  # 测试屏幕成功
        state = 'TestSuccess'
        # 2.记录测试通过信息
        result = TestRecordSV().buildTestRecord(jobnumber, testOptionCode, state)
        return result.toJson()
    if int(code) == cacheData.getDetectSpace:  # 测试屏幕失败
        state = 'TestFailed'
        # 2.记录测试通过信息
        result = TestRecordSV().buildTestRecord(jobnumber, testOptionCode, state)
        return result.toJson()
    else:
        return BeanRet(info="保存失败", code="9999").toJson()


@alert.route('/screen/success.shtml', methods=['GET'])
def screenSuccess():
    jobnumber = CacheData().toObj(CacheDataClient().read()).getJobnumber
    testOptionCode = 'screen'
    state = 'TestSuccess'

    if str(jobnumber).strip() and str(testOptionCode).strip() and str(state).strip():
        result = TestRecordSV().buildTestRecord(jobnumber, testOptionCode, state)
        return result.toJson()
    else:
        return BeanRet(info="保存失败", code="9999").toJson()


@alert.route('/screen/failed.shtml', methods=['GET'])
def sreccnFailed():
    jobnumber = CacheData().toObj(CacheDataClient().read()).getJobnumber
    testOptionCode = 'screen'
    state = 'TestFailed'
    if str(jobnumber).strip() and str(testOptionCode).strip() and str(state).strip():
        result = TestRecordSV().buildTestRecord(jobnumber, testOptionCode, state)
        return result.toJson()
    else:
        return BeanRet(info="保存失败", code="9999").toJson()
